<?php
/**
 * 涂医生管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-30
 */
namespace Admin\Controller;

class DoctorController extends CommonController{
    /*
     * 构造函数
     * 
     * @return  #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db           =new \Model\DoctorModel();
        $this->db_doc       =new \Model\DoctorquesttypeModel();
        $this->db_com       =new \Model\CommentModel();       
    }
    
    /*
     * 涂医生论坛正常发表文章列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.title")){
            $map['title']   =I("request.title");
        }
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Doctor");
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']   =2;
        if(session('type')<2){
            $map['userId']  =session("authId");
        }
        $sortBy         ='top';
        $sotr           =desc;
        $docList        =$this->_list($this->db,$map,$sortBy,$sotr);
        $this->assign("docList",$docList);
        $this->assign('act',1);       
        $this->display();
    }
    
    /*
     * 涂医生论坛待审列表
     * 
     * @return #
     */
    public function auditDocInfo(){
        if(I("request.title")){
            $map['title']   =I("request.title");
        }
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Doctor");
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']   =1;
        if(session('type')<2){
            $map['userId']  =session("authId");
        }
        $sortBy ='top';
        $sort   =desc;
        $docList=$this->_list($this->db,$map,$sortBy,$sort);
        $this->assign("docList",$docList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 审核、批量审核帖子
     * 
     * @return bool
     */
    public function alledit(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db->edits($map)){            
            $this->success("审核成功",U("index"));
        }else{
            $this->error("审核失败",U("index"));
        }
    }
    
    /*
     * 显示新增帖子页面
     * 
     * @return #
     */
    public function dShow(){
        $docQuestList=$this->db_doc->select();
        $this->assign("docType",$docQuestList);
        if(I("request.id")){
            $info=$this->db->find(I("request.id"));
            $this->assign("info",$info);
        }
        $this->display();
    }
    
    /*
     * 编辑新增帖子
     * 
     * @return #
     */
    public function dEdit(){
        if(I("request.id")){
            $map['id']      =I("request.id");
        }
        if(I("request.title")){
            $map['title']   =I("request.title");
        }else{
            $this->error("帖子标题不能为空");
        }
        if("request.content"){
            $map['content'] =I("request.content");
        }else{
            $this->error("帖子内容不能为空");
        }
        if(I("request.top")){
            $map['top_sign']=1;
        }
        if(I("request.image")){
            $map['image']   =I("request.image");
        }
        if(I("request.reward")){
            $map['reward']  =I("request.reward");
        }
        if(I("request.image1")){
            $map['image']   =I("request.image1");
        }
        if(I("request.image2")){
            if($map['image']){
                $map['image']   =$map['image'].','.I("request.image2");
            }else{
                $map['image']   =I("request.image2");
            }
        }
        if(I("request.image3")){
            if($map['image']){
                $map['image']   =$map['image'].','.I("request.image3");
            }else{
                $map['image']   =I("request.image3");
            }
        }
        $map['userId']      =session("authId");
        $map['type']        =I("request.type");
        if($this->db->edit($map)){
            $this->success("帖子编辑成功");
        }else{
            $this->error("帖子编辑失败");
        }
    }
    
    /*
     * 待审核，批量待审核
     * 
     * @return #
     */
    public function audit(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db->edits($map)){            
            $this->success("待审核成功",U("index"));
        }else{
            $this->error("待审核失败",U("index"));
        }
    }
    
    /*
     * 删除，批量删除帖子
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 涂医生分类列表
     * 
     * @return #
     */
    public function docQuestTypeList(){
        if(I("request.name")){
            $map['name']=I("request.name");
        }
        $map    =$this->_search("Doctorquesttype");
        $docType=$this->_list($this->db_doc,$map);
        $this->assign("docTye",$docType);
        $this->display();
    }
    
    /*
     * 显示涂医生分类编辑
     * 
     * @return #
     */
    public function show(){
        if(I("request.id")){
           $docTypeInfo =$this->db_doc->find(I("request.id","","int"));
           $this->assign("docTypeInfo",$docTypeInfo);
        }
        $this->display();
    }
    
    /*
     * 编辑涂医生帖子分类
     * 
     * @return #
     */
    public function edit(){
        if(I('request.id')){
            $map['id']  =I("request.id","","int");
        }
        if(I("request.name")){
            $map['name']    =I("request.name","","trim");
        }else{
            $this->error("分类名称不能为空");
        }
        if($this->db_doc->edit($map)){
            $this->success("编辑成功");
        }else{
            $this->error("编辑失败");
        }
    }
    
    /*
     * 删除、批量删除涂医生分类列表
     * 
     * @return #
     */
    public function delType(){
        $map['id']  =I("request.id");
        if($this->db_doc->del($map)){
            $this->success("涂医生分类删除成功");
        }else{
            $this->error("涂医生分类删除失败");
        }
    }
    
    /*
     * 涂医生论坛正常回复表
     * 
     * @return #
     */
    public function comment(){
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Comment");
        if("request.title"){
            $map['forumId'] =A("Common")->getIds(I("request.title"),"title","Doctor");
        }
        if(I("request.doc_id")){
            $map['forumId'] =I("request.doc_id");
        }
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']   =2;
        $sortBy         ='top';
        $sort           =desc;
        $commentList    =$this->_list($this->db_com,$map,$sortBy,$sort);
        $this->assign("com",$commentList);
        $this->assign("act",1);
        $this->display();
    }
    
    /*
     * 待审核的帖子回复
     * @return  #
     */
    public function auditComment(){
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Comment");
        if(I("request.title")){
            $map['forumId'] =A("Common")->getIds(I("request.title"),"title","Doctor");
        }
        if(I("request.nickName")){
            $map['userId']  =A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        $map['state']       =1;
        $sortBy             ='top';
        $sort               =desc;
        $auditCommentList   =$this->_list($this->db_com,$map,$sortBy,$sort);
        $this->assign("com",$auditCommentList);
        $this->assign('act',2);
        $this->display("comment");
    }
    
    /*审核，批量审核帖子回复
     * 
     * @return #
     */
     public function alleditCom(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db_com->edits($map)){            
            $this->success("审核成功",U("index"));
        }else{
            $this->error("审核失败",U("index"));
        }
    }
    
    /*
     * 待审核，批量待审核帖子回复
     * 
     * @return #
     */
    public function auditCom(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db_com->edits($map)){            
            $this->success("待审核成功",U("index"));
        }else{
            $this->error("待审核失败",U("index"));
        }
    }
    
    /*
     * 删除，批量删除帖子回复
     * 
     * @return #
     */
    public function delCom(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db_com->edits($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 上传图片
     * 
     * @return #
     */
    public function photo(){
        if(!I("request.id")){
            $this->error("参数错误");
        }
         $_SESSION['doc_img']=I("request.id");
         $this->display();
    }
    
    /*
     * 图片上出啊处理
     * 
     * @return #
     */
    public function docphoto(){
        if(!empty($_FILES['images']['name'])){            
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                           //根目录
                'savePath'  =>'doc/',                                                  //保存路径
                'maxSize'   =>'902400',                                                 //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")  //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['images']);
            if(!$upload){
                echo $new_upload->getError();
            }else{
                echo($upload['savepath'].$upload['savename']);                
            }           
        }
    }
}
